package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.TransportCompanyRpcService;
import com.hunttown.mes.rpc.domain.TransportCompanyDTO;
import com.hunttown.mes.rpc.domain.query.TransportCompanyDTOQuery;
import com.hunttown.mes.service.TransportCompanyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.TransportCompanyConverts.convert;

/**
 * created by wangjunfu on 2019-05-19 01:20:10
 */
@Service
public class TransportCompanyRpcServiceImpl implements TransportCompanyRpcService {

    private final Logger logger = LoggerFactory.getLogger(TransportCompanyRpcServiceImpl.class);

    private final TransportCompanyService transportCompanyService;

    @Autowired
    public TransportCompanyRpcServiceImpl(TransportCompanyService transportCompanyService) {
        this.transportCompanyService = transportCompanyService;
    }

    @Override
    public TransportCompanyDTO insert(TransportCompanyDTO transportCompanyDTO) {
        try {
            return convert(transportCompanyService.insert(convert(transportCompanyDTO)));
        } catch (Exception e) {
            logger.error("TransportCompany create failed.", e);
            throw new RuntimeException("TransportCompany create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(TransportCompanyDTO transportCompanyDTO) {
        try {
            return transportCompanyService.updateInfoById(convert(transportCompanyDTO));
        } catch (Exception e) {
            logger.error("TransportCompany update by id failed.", e);
            throw new RuntimeException("TransportCompany update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(TransportCompanyDTOQuery transportCompanyDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportCompanyDTOQuery.getW_id() != null && transportCompanyDTOQuery.getW_id() > 0) {
                query.put("w_id", transportCompanyDTOQuery.getW_id());
            }

            if (transportCompanyDTOQuery.getIds() != null && !transportCompanyDTOQuery.getIds().equals("")) {
                query.put("ids", transportCompanyDTOQuery.getIds());
            }

            if (transportCompanyDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportCompanyDTOQuery.getStopFlag());
            }

            if (transportCompanyDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportCompanyDTOQuery.getDeleteFlag());
            }

            return transportCompanyService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("TransportCompany update by query failed.", e);
            throw new RuntimeException("TransportCompany update by query failed, Please order your code.", e);
        }
    }

    @Override
    public TransportCompanyDTO selectById(Integer id) {
        try {
            return convert(transportCompanyService.selectById(id));
        } catch (Exception e) {
            logger.error("TransportCompany select by id failed.", e);
            throw new RuntimeException("TransportCompany select by id failed, Please order your code.", e);
        }
    }

    @Override
    public TransportCompanyDTO selectObjByQuery(TransportCompanyDTOQuery transportCompanyDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (transportCompanyDTOQuery.getZone() != null && transportCompanyDTOQuery.getZone() > 0) {
                query.put("zone", transportCompanyDTOQuery.getZone());
            }

            if (StringUtils.isNotBlank(transportCompanyDTOQuery.getCompanyNameCn())) {
                query.put("companyNameCn", transportCompanyDTOQuery.getCompanyNameCn());
            }

            if (StringUtils.isNotBlank(transportCompanyDTOQuery.getCompanyName())) {
                query.put("companyName", transportCompanyDTOQuery.getCompanyName());
            }

            if (StringUtils.isNotBlank(transportCompanyDTOQuery.getLikeName())) {
                query.put("likeName", transportCompanyDTOQuery.getLikeName());
            }

            if (transportCompanyDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", transportCompanyDTOQuery.getStopFlag());
            }

            if (transportCompanyDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", transportCompanyDTOQuery.getDeleteFlag());
            }

            return convert(transportCompanyService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("TransportCompany select obj by query failed.", e);
            throw new RuntimeException("TransportCompany select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<TransportCompanyDTO> getListForPage(TransportCompanyDTOQuery transportCompanyDTOQuery) {
        Query query = Query.page(transportCompanyDTOQuery.getPn(), transportCompanyDTOQuery.getPageSize());

        if (transportCompanyDTOQuery.getCompanyType() != null) {
            query.put("companyType", transportCompanyDTOQuery.getCompanyType());
        }

        if (transportCompanyDTOQuery.getId() != null && transportCompanyDTOQuery.getId() > 0) {
            query.put("id", transportCompanyDTOQuery.getId());
        }

        if (transportCompanyDTOQuery.getIds() != null && !transportCompanyDTOQuery.getIds().equals("")) {
            query.put("ids", transportCompanyDTOQuery.getIds());
        }

        if (transportCompanyDTOQuery.getParentId() != null) {
            query.put("parentId", transportCompanyDTOQuery.getParentId());
        }

        if (StringUtils.isNotBlank(transportCompanyDTOQuery.getCompanyNameCn())) {
            query.put("companyNameCn", transportCompanyDTOQuery.getCompanyNameCn());
        }

        if (StringUtils.isNotBlank(transportCompanyDTOQuery.getCompanyName())) {
            query.put("companyName", transportCompanyDTOQuery.getCompanyName());
        }

        if (StringUtils.isNotBlank(transportCompanyDTOQuery.getLikeName())) {
            query.put("likeName", transportCompanyDTOQuery.getLikeName());
        }

        if (transportCompanyDTOQuery.getIsSelf() != null) {
            query.put("isSelf", transportCompanyDTOQuery.getIsSelf());
        }

        if (transportCompanyDTOQuery.getCatalogId() != null && transportCompanyDTOQuery.getCatalogId() != -9999) {
            query.put("catalogId", transportCompanyDTOQuery.getCatalogId());
        }

        if (StringUtils.isNotBlank(transportCompanyDTOQuery.getShowDate())) {
            query.put("showDate", transportCompanyDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(transportCompanyDTOQuery.getStartDate())) {
            query.put("startDate", transportCompanyDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(transportCompanyDTOQuery.getEndDate())) {
            query.put("endDate", transportCompanyDTOQuery.getEndDate());
        }

        if (StringUtils.isNotBlank(transportCompanyDTOQuery.getOrderBy())) {
            query.put("orderBy", transportCompanyDTOQuery.getOrderBy());
        }

        if (transportCompanyDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", transportCompanyDTOQuery.getStopFlag());
        }

        if (transportCompanyDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", transportCompanyDTOQuery.getDeleteFlag());
        }

        List<TransportCompanyDTO> list = convert(transportCompanyService.selectByQuery(query));
        Page<TransportCompanyDTO> page = Page.from(transportCompanyDTOQuery.getPn(), transportCompanyDTOQuery.getPageSize(), list);
        page.setTotalItems(transportCompanyService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return transportCompanyService.deleteById(id);
        } catch (Exception e) {
            logger.error("TransportCompany delete failed.", e);
            throw new RuntimeException("TransportCompany delete failed, Please order your code.", e);
        }
    }

    @Override
    public List<Map<String, String>> selectSimilarCompany(String similar, Integer companyType) {
        try {
            Query query = Query.page(1, 20);

            if (StringUtils.isNotBlank(similar)) {
                query.put("likeName", similar.trim());
            }

            if (companyType != null && companyType > 0) {
                query.put("modelType", companyType);
            }

            return transportCompanyService.selectSimilarCompany(query);

        } catch (Exception e) {
            logger.error("TransportCompany select similar user failed.", e);
            throw new RuntimeException("TransportCompany similar user failed, Please order your code.", e);
        }
    }
}